﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace MiniMax.Library
{
    public class ChatCompletion_Pro
    {
        RequestModel request;
        public ChatCompletion_Pro(string ApiKey,string GroupID) 
        {
            request = new RequestModel(ApiKey,GroupID);
            request.body.model = "abab6.5-chat";
            request.body.stream = true;
            request.body.bot_setting.Add(new BotSetting() { bot_name = "MM", content = "你是只能助理，叫MM，性格活泼开朗" });
            request.body.reply_constraints.sender_type = "BOT";
            request.body.reply_constraints.sender_name = "MM";
        }

        public ChatCompletion_Pro(string ApiKey, string GroupID,string model)
        {
            request = new RequestModel(ApiKey, GroupID);
            request.body.model = model;
            request.body.stream = true;
            request.body.bot_setting.Add(new BotSetting() { bot_name = "MM", content = "你是只能助理，叫MM，性格活泼开朗" });
            request.body.reply_constraints.sender_type = "BOT";
            request.body.reply_constraints.sender_name = "MM";
        }

        public ChatCompletion_Pro(string ApiKey, string GroupID, string model,List<BotSetting> botSettings)
        {
            request = new RequestModel(ApiKey, GroupID);
            request.body.model = model;
            request.body.stream = true;
            request.body.bot_setting.AddRange(botSettings);
            request.body.reply_constraints.sender_type = "BOT";
            request.body.reply_constraints.sender_name = "MM";
        }
        public ChatCompletion_Pro(string ApiKey, string GroupID, string model, List<BotSetting> botSettings,ReplyConstraints replyConstraints)
        {
            request = new RequestModel(ApiKey, GroupID);
            request.body.model = model;
            request.body.stream = true;
            request.body.bot_setting.AddRange(botSettings);
            request.body.reply_constraints = replyConstraints;
        }

    }

    #region Request
    public class RequestModel 
    {
        public RequestModel_Header header { get; set; }
        public UrlParams Url_params { get; set; }

        public RequestModel_Body body { get; set; }

        public RequestModel(string ApiKey, string groupId)
        {
            header = new RequestModel_Header(ApiKey);
            Url_params = new UrlParams(groupId);
            body = new RequestModel_Body();
        }
        
    }

    #region Header
    public class RequestModel_Header 
    {
        /// <summary>
        /// API密钥
        /// </summary>
        public string Authorization { get; set; } = "";

        /// <summary>
        /// Content-Type
        /// </summary>
        public string ContentType { get; set; } = "application/json";

        public RequestModel_Header()
        {
            
        }

        public RequestModel_Header(string ApiKey) 
        {
            Authorization = ApiKey;
        }
    }
    #endregion

    #region UrlParams
    public class UrlParams 
    {
        public string GroupId { get; set; } = "";
        public UrlParams(string groupid)
        {
            GroupId = groupid;
        }
    }
    #endregion

    #region Body
    public class RequestModel_Body 
    {
        /// <summary>
        /// 调用的模型名称
        /// </summary>
        public string model { get; set; } = "";

        /// <summary>
        /// 是否通过流式分批返回结果。如果设置为true，结果分批返回，两个换行分割分批返回结果
        /// </summary>
        public bool stream { get; set; } = true;

        /// <summary>
        /// 最大生成token数，需要注意的是，这个参数并不会影响模型本身的生成效果，而是仅仅通过以截断超出的token的方式来实现功能
        /// </summary>
        public Int64? tokens_to_generate { get;set; }

        /// <summary>
        /// 较高的值将使输出更加随机，而较低的值将使输出更加集中和确定(0,1]
        /// </summary>
        public float? temperature { get; set; }

        /// <summary>
        /// 采样方法，数值越小结果确定性越强；数值越大，结果越随机(0,1]
        /// </summary>
        public float? top_p { get; set; }

        /// <summary>
        /// 对输出中易涉及隐私问题的文本信息进行打码，目前包括但不限于邮箱、域名、链接、证件号、家庭住址等，默认true，即开启打码
        /// </summary>
        public bool mask_sensitive_info { get; set; } = true ;

        /// <summary>
        /// 对话内容
        /// </summary>
        public List<RequestBodyMessage> messages { get; set; } = new List<RequestBodyMessage>();

        /// <summary>
        /// 对每一个机器人的设定
        /// </summary>
        public List<BotSetting> bot_setting { get; set; } = new List<BotSetting> { };

        /// <summary>
        /// 模型回复要求
        /// </summary>
        public ReplyConstraints reply_constraints { get; set; } = new ReplyConstraints();
    }
    #endregion

    #region Message
    public class RequestBodyMessage 
    {
        /// <summary>
        /// 发送者的类型(USER|BOT|FUNCTION)
        /// </summary>
        public string sender_type { get; set; } = "";

        /// <summary>
        /// 发送者的名字
        /// </summary>
        public string sender_name { get; set; } = "";

        /// <summary>
        /// 消息内容
        /// </summary>
        public string text { get; set; } = "";
    }
    #endregion

    #region Bot_setting
    public class BotSetting 
    {
        /// <summary>
        /// 具体机器人的名字
        /// </summary>
        public string bot_name { get; set; } = "";

        /// <summary>
        /// 具体机器人的设定
        /// </summary>
        public string content { get; set; } = "";
    }
    #endregion

    #region Reply_constraints
    public class ReplyConstraints
    {
        /// <summary>
        /// 指定回复的角色类型,当前只支持 BOT机器人
        /// </summary>
        public string sender_type { get; set; } = "BOT";

        /// <summary>
        /// 指定回复的机器人名称
        /// </summary>
        public string sender_name { get; set; } = "";
    }
    #endregion

    #endregion

    #region Response
    public class ResponseModel 
    {
        /// <summary>
        /// 请求发起时间
        /// </summary>
        public Int64 created { get; set; }

        /// <summary>
        /// 请求指定的模型名称
        /// </summary>
        public string model { get; set; }

        /// <summary>
        /// 回复内容
        /// </summary>
        public string reply { get; set; }

        /// <summary>
        /// 输入命中敏感词
        /// </summary>
        public bool input_sensitive { get; set; }

        /// <summary>
        /// 输入命中敏感词类型，当input_sensitive为true时返回
        /// 取值为以下其一：1.严重违规 2.色情 3.广告 4.违禁 5.谩骂 6.暴恐 7.其他
        /// </summary>
        public Int64 input_sensitive_type { get; set; }

        /// <summary>
        /// 输出命中敏感词
        /// </summary>
        public bool output_sensitive { get; set; }

        /// <summary>
        /// 输出命中敏感词类型，当output_sensitive为true时返回,取值同input_sensitive_type
        /// </summary>
        public Int64 output_sensitive_type { get; set;}

        /// <summary>
        /// 所有结果
        /// </summary>
        public List<Choices> choices { get; set; }

        /// <summary>
        /// tokens数使用情况,流式场景下，增量数据包不含该字段；全量（最后一个）数据包含有该字段
        /// </summary>
        public Usage usage { get;set; }

        /// <summary>
        /// 本次请求的唯一标识，用于排查问题
        /// </summary>
        public string id { get; set; }

        /// <summary>
        /// 错误状态码和详情
        /// </summary>
        public BaseResp base_resp { get; set; }

    }

    #region Choices
    public class Choices 
    {
        /// <summary>
        /// 回复结果的具体内容
        /// </summary>
        public List<ResponseChoiceMessages> messages { get; set; }

        /// <summary>
        /// 排名
        /// </summary>
        public Int64 index { get; set; }

        /// <summary>
        /// 结束原因，枚举值
        /// 【stop】：接口返回了模型生成完整结果 【length】：模型生成结果超过配置的tokens_to_generate长度，内容被截断 
        /// 【max_output】：输入+模型输出内容超过模型最大能力限制
        /// </summary>
        public string finish_reason { get; set; }
    }
    #endregion

    #region Messages
    public class ResponseChoiceMessages
    {
        /// <summary>
        /// 回复者的类型
        /// </summary>
        public string sender_type { get; set; }

        /// <summary>
        /// 回复者的名称
        /// </summary>
        public string sender_name { get; set; }

        /// <summary>
        /// 回复者的文本
        /// </summary>
        public string text { get; set; }
    }
    #endregion

    #region Usage
    public class Usage 
    {
        public Int64 total_tokens { get; set; }
    }
    #endregion

    #region BaseResp
    public class BaseResp 
    {
        /// <summary>
        /// 状态码。【1000：未知错误】【1001：超时】【1002：触发RPM限流】【1004：鉴权失败】【1008：余额不足】【1013：服务内部错误】【1027：输出内容错误】【1039：触发TPM限流】【2013：输入格式信息不正常】
        /// </summary>
        public Int64 status_code { get; set; }

        /// <summary>
        /// 错误详情
        /// </summary>
        public string status_msg { get; set; }
    }
    #endregion

    #endregion
}
